package constant.milk.aptprice;

import java.math.BigDecimal;

import constant.milk.aptprice.bean.AptData;
import constant.milk.aptprice.database.DBSelectQuery;
import constant.milk.aptprice.database.DbAdapter;
import constant.milk.aptprice.dialog.LocationDialog;
import constant.milk.aptprice.dialog.LocationDialog.LocationCollBank;
import constant.milk.aptprice.dialog.NumberDialog;
import constant.milk.aptprice.dialog.NumberDialog.NumberCollBank;
import constant.milk.aptprice.util.MilkUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends SuperActivity
{
	private TextView jagaTextView;
	private TextView junseTextView;
	
	private View locationView;
	private TextView locationTextView;
	
	private View pungView;
	private TextView pungTextView;
	
	private TextView pungMoneyTextView;
	private TextView pungMoneyHanTextView;
	
	private TextView totalMoneyTextView;
	private TextView totalMoneyHanTextView;
	
	private View monthMoneyView;
	private TextView monthMoneyTextView;
	private TextView monthMoneyHanTextView;
	
	private TextView termTextView;
	
	private String title;
	private String title2;
	private String pung;
	
	private String monthMoney;
	private BigDecimal totalMoney;
	
	private int jagaJunseFlag;
	
	private boolean backFlag;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main_activity);
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		jagaTextView = (TextView) findViewById(R.id.mainJagaTextView);
		junseTextView = (TextView) findViewById(R.id.mainJunseTextView);
		
		locationView = findViewById(R.id.mainLocationView);
		locationTextView = (TextView) findViewById(R.id.mainLocationTextView);
		
		pungView = findViewById(R.id.mainPungView);
		pungTextView = (TextView) findViewById(R.id.mainPungTextView);
		
		pungMoneyTextView = (TextView) findViewById(R.id.mainPungMoneyTextView);
		pungMoneyHanTextView = (TextView) findViewById(R.id.mainPungMoneyHanTextView);
		
		totalMoneyTextView = (TextView) findViewById(R.id.mainTotalMoneyTextView);
		totalMoneyHanTextView = (TextView) findViewById(R.id.mainTotalMoneyHanTextView);
		
		monthMoneyView = findViewById(R.id.mainMonthMoneyView); 
		monthMoneyTextView = (TextView) findViewById(R.id.mainMonthMoneyTextView);
		monthMoneyHanTextView = (TextView) findViewById(R.id.mainMonthMoneyHanTextView);
		
		termTextView = (TextView) findViewById(R.id.mainTermTextView);
	}

	@Override
	protected void setOnClick()
	{
		jagaTextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				jagaTextView.setBackgroundResource(R.drawable.bg_purple);
				jagaTextView.setTextColor(Color.WHITE);
				
				junseTextView.setBackgroundResource(R.drawable.bg_white);
				junseTextView.setTextColor(Color.BLACK);
				
				jagaJunseFlag = 0;
				
				setMoneyData();
			}
		});
		
		junseTextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				jagaTextView.setBackgroundResource(R.drawable.bg_white);
				jagaTextView.setTextColor(Color.BLACK);
				
				junseTextView.setBackgroundResource(R.drawable.bg_purple);
				junseTextView.setTextColor(Color.WHITE);
				
				jagaJunseFlag = 1;
				
				setMoneyData();
			}
		});
		
		locationView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				LocationDialog dialog = new LocationDialog(superContext, new LocationCollBank()
				{
					@Override
					public void getLocation(String name)
					{
						title = name;
						
						LocationDialog dialog = new LocationDialog(superContext, new LocationCollBank()
						{
							
							@Override
							public void getLocation(String name)
							{
								title2 = name;
								
								locationTextView.setText(title + " " + name);
								
								setMoneyData();
							}
						}, name);
						
						dialog.show();
					}
				}, null);
				
				dialog.show();
			}
		});
		
		pungView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				NumberDialog dialog = new NumberDialog(superContext, new NumberCollBank()
				{
					
					@Override
					public void getNumber(String number)
					{
						pung = number;
						pungTextView.setText(number + " 평");
						
						setMoneyData();
					}
				}, NumberDialog.COUNT_FLAG);
				
				dialog.show();
			}
		});
		
		monthMoneyView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				NumberDialog dialog = new NumberDialog(superContext, new NumberCollBank()
				{
					
					@Override
					public void getNumber(String number)
					{
						monthMoney = number;
						monthMoneyTextView.setText(MilkUtil.makeStringWithComma(number) + " 원");
						
						monthMoneyHanTextView.setVisibility(View.VISIBLE);
						monthMoneyHanTextView.setText(MilkUtil.makeMoneyHangul(number) + "원");
						
						setTerm();
					}
				}, NumberDialog.MONEY_FLAG);
				
				dialog.show();
			}
		});
	}

	@Override
	protected void setData()
	{
		jagaJunseFlag = 0;
		jagaTextView.setBackgroundResource(R.drawable.bg_purple);
		junseTextView.setBackgroundResource(R.drawable.bg_white);
		
		totalMoney = BigDecimal.ZERO;

		title = "";
		title2 = "";
		locationTextView.setText("위치를 입력 해주세요.");
		
		pung = "20";
		pungTextView.setText("20 평");
		
		monthMoney = "0";
		monthMoneyTextView.setText("0원");
		monthMoneyHanTextView.setVisibility(View.GONE);
		
		setTerm();
	}
	
	private void setMoneyData()
	{
		if (title.equals("") || title2.equals(""))
		{
			return;
		}
		
		DBSelectQuery select = new DBSelectQuery(superContext, superDb);
		
		AptData data = select.getHouseTitleName(title, title2);
		
		BigDecimal pungMoney = BigDecimal.ZERO;
		
		if (jagaJunseFlag == 0)
		{
			pungMoney = new BigDecimal(data.getMeib());
		}
		else
		{
			pungMoney = new BigDecimal(data.getJunse());
		}
		
		pungMoney = new BigDecimal(pungMoney.multiply(new BigDecimal(3.3)).intValue());
		
		pungMoney = new BigDecimal(pungMoney.toString() + "0000");
		
		pungMoneyTextView.setText(MilkUtil.getWon(pungMoney.toString()) + " 원");
		pungMoneyHanTextView.setText(MilkUtil.makeMoneyHangul(pungMoney.toString()) + "원");
		
		BigDecimal pungsu = new BigDecimal(pung.toString());
		
		totalMoney = pungsu.multiply(pungMoney);
		
		totalMoneyTextView.setText(MilkUtil.getWon(totalMoney.toString()) + " 원");
		totalMoneyHanTextView.setText(MilkUtil.makeMoneyHangul(totalMoney.toString()) + "원");
		
		setTerm();
	}
	
	private void setTerm()
	{
		// 남은기간
		if (totalMoney.toString().equals("0"))
		{
			termTextView.setText("");
		}
		else if (monthMoney.equals("0"))
		{
			termTextView.setText("평생");
		}
		else
		{
			String resultCount = "0";
			BigDecimal count = totalMoney.divide(new BigDecimal(monthMoney), 0, BigDecimal.ROUND_UP);
			
			if (count.compareTo(new BigDecimal("12")) > 0)
			{
				BigDecimal year = count.divide(new BigDecimal("12"), 0, BigDecimal.ROUND_DOWN);
				BigDecimal month = count.remainder(new BigDecimal("12"));
				
				resultCount = year.toString() + " 년 ";
				
				if (month.compareTo(BigDecimal.ZERO) > 0)
				{
					resultCount = resultCount + month + " 개월";
				}
			}
			else
			{
				resultCount = count.toString() + " 개월";
			}
			
			termTextView.setText(resultCount);
		}
	}
	
	private Handler backHandler = new Handler()
    {
		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			
			if (msg.what == 0)
			{
				backFlag = false;
			}
		}
    };
	
	@Override
	public void onBackPressed()
	{
		if (!backFlag)
		{
			MilkUtil.showToast(superContext, "뒤로 버튼을 한번더 누르시면 종료됩니다.");
			backFlag = true;
			
			backHandler.sendEmptyMessageDelayed(0, 2000);
		}
		else
		{
			superDb = DbAdapter.getInstance();
    		superDb.close();
			super.onBackPressed();
		}
	}
}
